/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.admin.dto.ElevatorPartsRelationInsertDTO;
import org.springblade.modules.admin.entity.ElevatorPartsStructureRelation;
import org.springblade.modules.admin.entity.MaintainParts;
import org.springblade.modules.admin.service.MaintainPartsService;
import org.springblade.modules.admin.vo.ElevatorPartsStructureRelationVO;
import org.springblade.modules.admin.mapper.ElevatorPartsStructureRelationMapper;
import org.springblade.modules.admin.service.IElevatorPartsStructureRelationService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 电梯配件结构关联配件信息表 服务实现类
 *
 * @author ysq
 * @since 2024-10-11
 */
@Service
public class ElevatorPartsStructureRelationServiceImpl extends BaseServiceImpl<ElevatorPartsStructureRelationMapper, ElevatorPartsStructureRelation> implements IElevatorPartsStructureRelationService {

	@Autowired
	private MaintainPartsService maintainPartsService;

	@Override
	public IPage<ElevatorPartsStructureRelationVO> selectElevatorPartsStructureRelationPage(IPage<ElevatorPartsStructureRelationVO> page, ElevatorPartsStructureRelationVO elevatorPartsStructureRelation) {
		return page.setRecords(baseMapper.selectElevatorPartsStructureRelationPage(page, elevatorPartsStructureRelation));
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean relationInsert(ElevatorPartsRelationInsertDTO relation) {
		MaintainParts parts = maintainPartsService.getById(relation.getPartsId());
		if (parts == null) {
			throw new ServiceException("配件结构不存在");
		}
		if (parts.getFlag() > 0 && Func.isEmpty(relation.getFloorStandTemplateId())) {
			throw new ServiceException("层站ID为空");
		}

		LambdaQueryWrapper<ElevatorPartsStructureRelation> wrapper = Wrappers.<ElevatorPartsStructureRelation>lambdaQuery()
			.eq(ElevatorPartsStructureRelation::getElevatorId, relation.getElevatorId())
			.eq(ElevatorPartsStructureRelation::getPartsId, relation.getPartsId());
		if (parts.getFlag() > 0) {
			wrapper.eq(ElevatorPartsStructureRelation :: getFloorStandTemplateId, relation.getFloorStandTemplateId());
		} else {
			wrapper.isNull(ElevatorPartsStructureRelation::getFloorStandTemplateId);
		}
		List<ElevatorPartsStructureRelation> oldList = list(wrapper);

		List<ElevatorPartsStructureRelation> saveRelationList = new ArrayList<>();
		Date createTime = oldList.isEmpty() ? new Date() : oldList.get(0).getCreateTime();
		for (ElevatorPartsRelationInsertDTO.SkuInfo skuInfo : relation.getSkuInfoList()) {
			if (skuInfo.getSkuId() == null || Func.isBlank(skuInfo.getSkuAttr())) {
				throw new ServiceException("skuId或skuAttr为空");
			}
			ElevatorPartsStructureRelation saveRelation = new ElevatorPartsStructureRelation();
			saveRelation.setElevatorId(relation.getElevatorId());
			saveRelation.setPartsId(relation.getPartsId());
			saveRelation.setFloorStandTemplateId(relation.getFloorStandTemplateId());
			saveRelation.setSkuId(skuInfo.getSkuId());
			saveRelation.setSkuInfo(skuInfo.getSkuAttr());
			saveRelation.setCreateTime(createTime);
			saveRelationList.add(saveRelation);
		}

		// 需删除原有关联配件id
		List<Long> removeIds = oldList.stream().map(ElevatorPartsStructureRelation::getId).collect(Collectors.toList());
		boolean delFlag = removeIds.isEmpty() || removeByIds(removeIds);
		// 插入
		boolean saveFlag = saveRelationList.isEmpty() || saveBatch(saveRelationList);
		return  delFlag && saveFlag;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean batchRelationInsert(List<ElevatorPartsRelationInsertDTO> params) {
		for (ElevatorPartsRelationInsertDTO param : params) {
			if (Func.hasEmpty(param.getElevatorId(), param.getPartsId())) {
				throw new ServiceException("缺少必要参数");
			}
			boolean flag = relationInsert(param);
			if (!flag) {
				throw new ServiceException("批量关联失败,请重试！");
			}
		}
		return true;
	}
}
